package com.coastwe.dahua.chapter04;

public class LinkQueue {
    
    private Node head;
    private Node tail;

    public LinkQueue() {
        head = tail = null;
    }

    public boolean enqueue(int value) {
        Node p = createNode(value);
        if (tail == null) {
            tail = p;
            head = p;
        } else {
            tail.next = p;
            tail = p;
        }
        return true;
    }

    public int dequeue() {
        if (head == null) {
            return -1;
        }
        int value = head.data;
        Node temp = head.next;
        if (head == tail) {
            tail = temp;
        }
        head = temp;
        return value;
    }

    public static void main(String[] args) {
        LinkQueue queue = new LinkQueue();
        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);
        queue.enqueue(4);
        queue.enqueue(5);
        System.out.print(queue.dequeue() + " ");
        System.out.print(queue.dequeue() + " ");
        System.out.print(queue.dequeue() + " ");
        System.out.print(queue.dequeue() + " ");
        System.out.print(queue.dequeue() + " ");
        System.out.print(queue.dequeue() + " ");
        System.out.println("出列===");
        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);
        queue.enqueue(4);
        queue.enqueue(5);
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
    }


    private static Node createNode(int value) {
        return new Node(value, null);
    }

    private static class Node {
        int data;
        Node next;

        public Node(int value, Node next) {
            this.data = value;
            this.next = next;
        }
    }
}